#include <iostream>
#include <stack>
using namespace std;

int least_add(const string &seq)
{
    stack<char> S;
    int addition = 0;
    for (char ch : seq)
    {
        if (ch == '(' || ch == '{' || ch == '[')
            S.push(ch);
        else
        {
            char top = S.empty() ? 'E' : S.top();
            switch (ch)
            {
            case ')':
                if (top == '(')
                    S.pop();
                else
                    addition++;
                break;

            case ']':
                if (top == '[')
                    S.pop();
                else
                    addition++;
                break;
            case '}':
                if (top == '{')
                    S.pop();
                else
                    addition++;
                break;
            }
        }
    }
    return addition + S.size();
}

int main()
{
    while (true)
    {
        string seq;
        cout << "请输入一组括号序列：";
        cin >> seq;
        cout << "需要添加最少：" << least_add(seq) << "个括号，才可成为合法序列\n"
             << endl;
    }
    return 0;
}